import { Column, Entity, JoinTable, ManyToMany, OneToMany } from 'typeorm'
import { Menu } from './menu.entity'
import { Operate } from './operate.entity'
import { Permission } from './permission.entity'
import { UserRole } from './user-role.entity'

@Entity()
export class Role extends Operate {
  @Column({ name: 'role_name', type: 'varchar', length: 16, comment: '角色名称' })
  roleName: string

  @Column({ name: 'role_code', type: 'varchar', unique: true, length: 64, comment: '角色code' })
  roleCode: string

  @Column({
    type: 'enum',
    enum: ['1', '2'],
    default: '1',
    comment: '状态1、可用，2、禁用',
  })
  status: string

  @Column({ name: 'role_desc', nullable: true, comment: '角色详情' })
  roleDesc: string | null

  @Column({ type: 'tinyint', default: 1, comment: '角色类型1、公共角色，2、特殊角色' })
  type: number

  @OneToMany(() => UserRole, (userRole) => userRole.role)
  users: UserRole[]

  @ManyToMany(() => Menu, (menu) => menu.roles)
  @JoinTable({
    name: 'role_menu',
    joinColumn: { name: 'role_id' },
    inverseJoinColumn: { name: 'menu_id' },
  })
  menus: Menu[]

  @ManyToMany(() => Permission, (permission) => permission.roles, {
    onDelete: 'CASCADE',
  })
  @JoinTable({
    name: 'role_permission',
    joinColumn: { name: 'role_id' },
    inverseJoinColumn: { name: 'permission_id' },
  })
  permissions: Permission[]
}
